#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: messages.py
@contact: zwb15083976291@163.com
@time: 2022/03/10
"""
from django.db import transaction
from django.utils.decorators import method_decorator

from admin.serializers.messages import MessagesPaginatorSerializer, MessagesSerializers, MessagesBatchSerializers
from common.models import BlogMessage
from utils.responses import CommonResponse, CommonAPIResponseStatus
from utils.user_authentication import allow_admin_user
from utils.views.views import BasePaginateGenericAPIView, BaseGenericAPIView


@method_decorator(allow_admin_user, name='dispatch')
class MessagesView(BasePaginateGenericAPIView):
    diff_methods_serializer_class = dict(get=MessagesPaginatorSerializer, delete=MessagesSerializers,
                                         put=MessagesSerializers)
    diff_methods_request_params = dict(
        get=dict(optional=['pageNum', 'pageSize', 'audit'], defualt_value=dict(pageNum=1, pageSize=10)),
        delete=dict(required=['id']),
        put=dict(required=['id', 'audit'])
    )

    def get(self, request):
        ret, msg = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=msg)
        query_set = BlogMessage.objects.order_by("-create_time")
        if self.serializer.validated_data.get('audit', None) is not None:
            query_set = query_set.filter(audit=self.serializer.validated_data.get('audit'))
        paginator, models = self.paginate_queryset(query_set, self.serializer.validated_data)
        return CommonResponse(data=self.get_paginated_response(self.serializer.validated_data, paginator,
                                                               MessagesSerializers(instance=models, many=True).data))

    def delete(self, request):
        ret, msg = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=msg)
        self.serializer.select_field_delete_obj()
        return CommonResponse(message="删除成功")

    def put(self, request):
        ret, msg = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=msg)
        self.serializer.select_field_update_obj()
        return CommonResponse(message="修改成功")


@method_decorator(allow_admin_user, name='dispatch')
class MessagesBatchView(BaseGenericAPIView):
    diff_methods_serializer_class = dict(delete=MessagesBatchSerializers, put=MessagesBatchSerializers)
    diff_methods_request_params = dict(
        delete=dict(required=['ids']),
        put=dict(required=['ids', 'audit'])
    )

    def delete(self, request):
        ret, msg = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=msg)
        # 批量删除
        ids = self.serializer.validated_data.get("ids")
        BlogMessage.objects.filter(id__in=ids).delete()
        return CommonResponse(message="批量删除成功")

    def put(self, request):
        ret, msg = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=msg)
        # 批量修改
        ids = self.serializer.validated_data.get("ids")
        audit = self.serializer.validated_data.get("audit")
        BlogMessage.objects.filter(id__in=ids).update(audit=audit)
        return CommonResponse(message="批量修改成功")
